package cn.stylefeng.guns.sys.modular.system.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.base.pojo.node.LayuiTreeMouldNode;
import cn.stylefeng.guns.base.pojo.node.LayuiTreeNode;
import cn.stylefeng.guns.base.pojo.node.TreeviewNode;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.modular.system.entity.MouldBoard;
import cn.stylefeng.guns.sys.modular.system.entity.MouldSubtable;
import cn.stylefeng.guns.sys.modular.system.mapper.MouldBoardMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.MouldSubtableParam;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 模板表 服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2018-12-07
 */
@Service
public class MouldBoardService extends ServiceImpl<MouldBoardMapper, MouldBoard> {

    @Resource
    private MouldBoardMapper mouldBoardMapper;

    @Resource
    private MouldSubtableService mouldSubtableService;

    /**
     * 新增模板
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:00 PM
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean addMouldBoard(MouldBoard mouldBoard) {

        if (ToolUtil.isOneEmpty(mouldBoard, mouldBoard.getPid())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }

        //完善pids,根据pid拿到pid的pids
        this.mouldBoardSetPids(mouldBoard);

        if(CollectionUtil.isNotEmpty(mouldBoard.getMouldSubtableList())){

            for(MouldSubtable mouldSubtable:mouldBoard.getMouldSubtableList()){
                MouldSubtableParam mouldSubtableParam=new MouldSubtableParam();
                BeanUtil.copyProperties(mouldSubtable, mouldSubtableParam);
                mouldSubtableService.add(mouldSubtableParam);
            }
        }

       return this.save(mouldBoard);
    }

    /**
     * 修改模板
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:00 PM
     */
    @Transactional(rollbackFor = Exception.class)
    public void editMouldBoard(MouldBoard mouldBoard) {

        if (ToolUtil.isOneEmpty(mouldBoard, mouldBoard.getId(), mouldBoard.getSimpleName(), mouldBoard.getFullName(), mouldBoard.getPid())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }

        //完善pids,根据pid拿到pid的pids
        this.mouldBoardSetPids(mouldBoard);

        this.updateById(mouldBoard);
    }

    /**
     * 删除模板
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:16 PM
     */
    @Transactional
    public void deleteMouldBoard(Long mouldBoardId) {
        MouldBoard mouldBoard = mouldBoardMapper.selectById(mouldBoardId);



        this.removeById(mouldBoard.getId());
    }

    /**
     * 获取ztree的节点列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:16 PM
     */
    public List<ZTreeNode> tree() {
        return this.baseMapper.tree();
    }

    /**
     * 获取layuiTree的节点列表
     *
     * @author fengshuonan
     * @Date 2019-8-27 15:23
     */
    public List<LayuiTreeNode> layuiTree() {
        return this.baseMapper.layuiTree();
    }

    /**
     * 获取layuiTree的节点列表
     *
     * @author fengshuonan
     * @Date 2019-8-27 15:23
     * @param mouldId
     */
    public List<LayuiTreeMouldNode> layuiMouldTree(Long mouldId) {
        return this.baseMapper.layuiMouldTree(mouldId);
    }

    /**
     * 获取部門名稱
     */
    public String name(Long mouldBoardId){
        return this.baseMapper.name(mouldBoardId);
    }

    /**
     * 获取ztree的节点列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:16 PM
     */
    public List<TreeviewNode> treeviewNodes() {
        return this.baseMapper.treeviewNodes();
    }

    /**
     * 获取所有模板列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:16 PM
     */
    public Page<Map<String, Object>> list(String condition, Long mouldBoardId) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.list(page, condition, mouldBoardId);
    }

    /**
     * 设置模板的父级ids
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:58 PM
     */
    private void mouldBoardSetPids(MouldBoard mouldBoard) {
        if (ToolUtil.isEmpty(mouldBoard.getPid()) || mouldBoard.getPid().equals(0L)) {
            mouldBoard.setPid(0L);
            mouldBoard.setPids("[0],");
        } else {
            Long pid = mouldBoard.getPid();
            //MouldBoard temp = this.getById(pid);


            MouldBoard temp=mouldBoardMapper.selectById(pid);
            String pids = temp.getPids();
            mouldBoard.setPid(pid);
            mouldBoard.setPids(pids + "[" + pid + "],");
        }
    }

    public List<MouldBoard> view(Long id) {
        List<MouldBoard> list = mouldBoardMapper.likePids(id);
        MouldBoard mouldBoard = mouldBoardMapper.selectById(id);
        list.add(mouldBoard);
        return list;
    }

}
